﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThesisSystem.Models;
using ThesisSystem.DAL;

namespace ThesisSystem.Controllers
{ 
    public class GClassController : Controller
    {
        private ThesisSystemEntities db = new ThesisSystemEntities();

        //
        // GET: /Default1/

        public ViewResult Index(int? departmentID)
        {
            var gclasses = db.GClasses.Include(g => g.Department);
            if (departmentID != null)
            {
                gclasses = gclasses.Where(g => g.DepartmentID == departmentID);
            }
            return View(gclasses.ToList());
        }

        //
        // GET: /Default1/Details/5

        public ViewResult Details(int id)
        {
            GClass gclass = db.GClasses.Find(id);
            return View(gclass);
        }

        //
        // GET: /Default1/Create

        public ActionResult Create()
        {
            ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DepartmentName");
            return View();
        } 

        //
        // POST: /Default1/Create

        [HttpPost]
        public ActionResult Create(GClass gclass)
        {
            if (ModelState.IsValid)
            {
                db.GClasses.Add(gclass);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DepartmentName", gclass.DepartmentID);
            return View(gclass);
        }
        
        //
        // GET: /Default1/Edit/5
 
        public ActionResult Edit(int id)
        {
            GClass gclass = db.GClasses.Find(id);
            ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DepartmentName", gclass.DepartmentID);
            return View(gclass);
        }

        //
        // POST: /Default1/Edit/5

        [HttpPost]
        public ActionResult Edit(GClass gclass)
        {
            if (ModelState.IsValid)
            {
                db.Entry(gclass).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DepartmentName", gclass.DepartmentID);
            return View(gclass);
        }

        //
        // GET: /Default1/Delete/5
 
        public ActionResult Delete(int id)
        {
            GClass gclass = db.GClasses.Find(id);
            return View(gclass);
        }

        //
        // POST: /Default1/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            GClass gclass = db.GClasses.Find(id);
            db.GClasses.Remove(gclass);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}